import 'package:flutter/material.dart';

/// 页面跳转
/// 1. 配置路由
/// 2. 使用Navigator.pushNamed方法跳转
/// 3. 使用Navigator.pop方法回退

class NameRouteApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final String appTitle = 'Name Route Demo';
    return MaterialApp(
      title: appTitle,
      theme: ThemeData(primarySwatch: Colors.red),
//      home: NameRouteAppPage(title: appTitle),
      // 配置初始化路由后则不用配置home属性
      initialRoute: '/',
      routes: {
        '/': (context) => NameRouteAppPage(title: appTitle),
        '/second': (context) => SecondScreen()
      },
    );
  }
}

class NameRouteAppPage extends StatefulWidget {
  final String title;

  NameRouteAppPage({Key key, this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() => NameRouteAppPageState();
}

class NameRouteAppPageState extends State<NameRouteAppPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: RaisedButton(
            child: Text('Launch screen'),
            onPressed: () {
              // 跳转页面
              Navigator.pushNamed(context, '/second');
            }),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Second Screen'),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            // 回退
            Navigator.pop(context);
          },
          child: Text('Go back'),
        ),
      ),
    );
  }
}
